// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioManage, {soundList} from "../framework/AudioManage";
import settingMediator from "../framework/settingMediator";
import Tool from "../framework/Tool";

const {ccclass, property} = cc._decorator;

export enum defaultType{
    stone = 0,
    box = 1,
    moveTable = 2,
    stab = 3
}

@ccclass
export default class defaultTypeMediator extends cc.Component {

    @property({
        type : cc.Enum(defaultType),
        displayName : "当前默认类型"
    })
    curType : defaultType = defaultType.stone;

    @property()
    moveSpeed : cc.Vec2 = cc.v2(0,0);

    isCheck : boolean = false;

    isPlay : boolean = false;

    initPos = null;

    isOpenMove : boolean = false;

    onLoad(){
        this.initPos = this.node.position;
    }

    protected onDestroy(): void {
        AudioManage.playSFX(soundList.pushBox,true,true,1);
    }

    onBeginContact(contact, self, other) {

        if(this.curType == defaultType.stone){
            if(this.node.position.y - this.initPos.y < -50){
                console.log("石头掉落的和发声");
                AudioManage.playSFX(soundList.stoneFall,false);
                settingMediator.doVibration(1);
                this.initPos = this.node.position;
            }
        }

        if(this.curType == defaultType.box){
            if(other.node.group == "role"){
                if(this.isCheck) return;
                console.log("2222");
                this.isCheck = true;
            }
            if(this.node.position.y - this.initPos.y < -50){
                console.log("箱子掉落的和发声");
                AudioManage.playSFX(soundList.boxLand,false);
                settingMediator.doVibration(1);
                this.initPos = this.node.position;
            }
        }

        if(this.curType == defaultType.moveTable && other.node.group == "role"){
            Tool.loadPrefab("prefabs/spine",(res)=>{
                let view = cc.instantiate(res);
                view.x = this.node.x;
                view.y = this.node.y;
                view.parent = cc.find("Canvas");
                this.node.destroy();
            });
        }
    }

    openMoveState(){
        this.isOpenMove = true;
    }

    onEndContact(contact, self, other) {
        if(this.curType == defaultType.box){
            if(other.node.group == "role"){
                this.isCheck = false;
                if(this.isPlay){
                    // AudioManage.playSFX(soundList.pushBox,true,true,1);
                    this.isPlay = false;
                }
            }
        }

        if(other.node.group == "ghost" && this.curType == defaultType.box){
            console.log("1111");
            this.node.getComponent(cc.PhysicsBoxCollider).sensor = false;
        }
    }


    protected update(dt: number): void {
        if(this.isCheck){
            let speed = this.node.getComponent(cc.RigidBody).linearVelocity;
            if(speed.x > 10 || speed.y > 10){
                if(!this.isPlay){
                    console.log("!!!!");
                    // AudioManage.playSFX(soundList.pushBox,true,false,1);
                    this.isPlay = true;
                }
            }else{
                if(this.isPlay){
                    // AudioManage.playSFX(soundList.pushBox,true,true,1);
                    this.isPlay = false;
                }
            }
        }
        if(this.curType == defaultType.moveTable && this.isOpenMove){
            this.node.x += this.moveSpeed.x;
            this.node.y += this.moveSpeed.y;
        }
    }
}
